package imse.team0.processors;

import imse.team0.orders.Order;
import imse.team0.orders.SplitOrderItem;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 4/29/12
 * Time: 12:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class OrderItemsAggregationStrategy implements AggregationStrategy {

    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        SplitOrderItem newBody = newExchange.getIn().getBody(SplitOrderItem.class);
        Order order = null;
        if (oldExchange == null) {
            order = new Order();
            order.orderid = newBody.orderid;
            order.clientID = newBody.clientID;
            order.receiptTime = newBody.receiptTime;
            order.items = new ArrayList<Order.OrderItem>();
            Order.OrderItem item = new Order.OrderItem(newBody.itemId, newBody.quantity);
            order.items.add(item);
            newExchange.getIn().setBody(order);
            return newExchange;
        } else {
            order = oldExchange.getIn().getBody(Order.class);
            order.items.add(new Order.OrderItem(newBody.itemId, newBody.quantity));
            return oldExchange;
        }
    }
}
